package servlet;

import com.alibaba.fastjson.JSON;
import dao.DAO;
import domain.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.io.PrintWriter;

@WebServlet("/CustomerServlet")  //ajax访问路径
public class CustomerServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type=request.getParameter("type");
        if("update".equals(type)){
            customer_update(request,response);
        }
    }

    private void customer_update(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        User user = new User();

        try {
            String user_ids=request.getParameter("user_id");
            String user_account=request.getParameter("user_account");
            String user_password=request.getParameter("user_password");
            String user_name=request.getParameter("user_name");
            String user_ages=request.getParameter("user_age");
            String user_sex=request.getParameter("user_sex");
            String user_address=request.getParameter("user_address");
            String user_contact=request.getParameter("user_contact");


            int user_id=Integer.parseInt(user_ids);//转换为整数
            int user_age=Integer.parseInt(user_ages);//转换为整数
            user.setUser_id(user_id);
            user.setUser_account(user_account);
            user.setUser_password(user_password);
            user.setUser_name(user_name);
            user.setUser_age(user_age);
            user.setUser_sex(user_sex);
            user.setUser_address(user_address);
            user.setUser_contact(user_contact);
        } catch (Exception e) {
            e.printStackTrace();
        }

        DAO dao = new DAO();
        int result = 0;
        result = dao.updateCustomer();

        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;

        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (result > 0) {
            out.print("个人信息修改成功");
        } else {
            out.print("个人信息修改失败");
        }
        out.flush();
        out.close();
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
//    public void getAllGoods(HttpServletRequest request, HttpServletResponse response) {
//
//    }
//    public void queryGoods(HttpServletRequest request, HttpServletResponse response){
//
}







